package com.bonait.hotapp.utils;

import android.annotation.SuppressLint;
import android.os.Environment;
import android.util.Log;
import com.bonait.hotapp.entity.vo.UpdateVO;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class UpdateUtil {

    private UpdateListener updateListener;
    private final String SAVE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "patch"; // 储存下载文件的路径
    private String appUrl = "https://gwdl.coral3.com/patch/patch_signed_7zip.apk";
    private String resUrl = "https://gwdl.coral3.com/patch/patch_signed_7zip.apk";
    private File file;
    private static UpdateUtil updateUtil;

    public UpdateUtil(UpdateListener updateListener) {
        this.updateListener = updateListener;
    }

    public static UpdateUtil newInstance(UpdateListener updateListener){
        if(updateUtil == null) updateUtil = new UpdateUtil(updateListener);
        return updateUtil;
    }

    public interface UpdateListener {
        void DownloadSuccess(String resUrl);//成功返回
        void DownloadFailure(String err);//失败返回
    }

    private void update(){
        startDownload();
    }
    
    public void startDownload() {
        OkHttpDownload(getUpdateModel());
    }

    private UpdateVO getUpdateModel(){
        UpdateVO updateModel = new UpdateVO();
        updateModel.setAppUrl(appUrl);
        updateModel.setResUrl(resUrl);
        return updateModel;
    }

    /**
     * @author : wty
     * @time : 2020/11/5
     * @name : downloadFile
     * @Parameters : [url]
     * @describe :下载文件
     */
    @SuppressLint("NewApi")
    public void OkHttpDownload(UpdateVO updateModel) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(updateModel.getResUrl()).build();
        Log.d("yue-tag", "开始下载:" + updateModel.getResUrl());
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //请求失败
                updateListener.DownloadFailure("failure");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //请求成功
                InputStream inputStream = null;
                FileOutputStream fileOutputStream = null;
                Log.d("yue-tag", "请求成功");
                byte[] bytes = new byte[1024 * 10];
                int length = 0;
                file = new File(SAVE_PATH);
                if (!file.exists()) { //文件夹不存在
                    // 创建文件夹
                    file.mkdirs();
                }
                try {
                    inputStream = response.body().byteStream();
                    file = new File(SAVE_PATH, updateModel.getResName());
                    fileOutputStream = new FileOutputStream(file);
                    while ((length = inputStream.read(bytes)) != -1) {
                        fileOutputStream.write(bytes, 0, length);
                    }
                    fileOutputStream.flush();
                    //下载成功
                    updateListener.DownloadSuccess(file.getPath());
                } catch (Exception e) {
                    e.printStackTrace();
                    // 下载失败
                    updateListener.DownloadFailure("failure");
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }

                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                }
            }
        });
    }
}
